//
//  ChangePasswordController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ChangePasswordController.h"

@interface ChangePasswordController ()
@property (weak, nonatomic) IBOutlet UITextField *oldPassword;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation ChangePasswordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"修改密码";
    
    [self addRightButtonAction];
}
-(void)addRightButtonAction{
    UIButton* _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnRight setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnRight setTitle:@"保存" forState:UIControlStateNormal];
    [_btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_btnRight setBounds:CGRectMake(0, 0, 50, 44)];
    UIBarButtonItem* Rightitem  = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
    [_btnRight addTarget:self action:@selector(rightButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = Rightitem;
}
#pragma mark -点击确认修改密码
-(void)rightButtonClickAction{
    
    BmobUser *buser = [BmobUser currentUser];
    NSString *username = buser.username;
    [buser updateCurrentUserPasswordWithOldPassword:self.oldPassword.text newPassword:self.password.text block:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            [EasyTextView showSuccessText:@"修改密码成功"];
            //用新密码登录
            [BmobUser loginInbackgroundWithAccount:username andPassword:self.password.text block:^(BmobUser *user, NSError *error) {
                if (!error) {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }];
        } else {
            [EasyTextView showErrorText:@"修改密码失败"];

        }
    }];
    
    
}
@end
